package nc.bs.pub.action;

import java.util.ArrayList;
import java.util.List;

import nc.bs.common.projectFAUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.pu.m20.IPraybillApprove;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pu.m20.context.PraybillContext;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pu.m20.pub.PraybillContextUtil;
import nc.vo.pu.m422x.entity.StoreReqAppHeaderVO;
import nc.vo.pu.m422x.entity.StoreReqAppItemVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pub.power.PowerActionEnum;
import nc.vo.scmpub.res.billtype.POBillType;
import nc.vo.uap.pf.PFBusinessException;
import nccloud.vo.scmpub.utils.power.DataPermissionUtil;

public class N_20_APPROVE extends AbstractCompiler2 {
	public String getCodeRemark() {
		return "\tsuper.m_tmpVo = paraVo;\n      // ####本脚本必须含有返回值,返回DLG和PNL的组件不允许有返回值####\n      nc.vo.pu.m20.entity.PraybillVO[] inObject = (nc.vo.pu.m20.entity.PraybillVO[]) getVos();\n      Object retValue =nc.bs.framework.common.NCLocator.getInstance().lookup(nc.itf.pu.m20.IPraybillApprove.class).approve(inObject,this);\n      return retValue;\n";
	}

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			super.m_tmpVo = vo;
			PraybillVO[] inObject = (PraybillVO[]) ((PraybillVO[]) this.getVos());
			
			//lhw 231211 审批前校验项目 begin
			new projectFAUtils().projectDataApprove("po_praybill", inObject, "approve");
			//end
			
			PraybillContext[] ptxs = PraybillContextUtil.getPtxs(vo);
			if (vo.isCloudEntry) {
				DataPermissionUtil.checkPermission(inObject, POBillType.PrayBill.getCode(),
						PowerActionEnum.APPROVE.getActioncode(), "vbillcode");
			}

			 PraybillVO[] retValue = ((IPraybillApprove) NCLocator.getInstance().lookup(IPraybillApprove.class))
					.approve(inObject, this, ptxs);
			
			//lxiang20231113 审批通过后回写转请购执行人  转请购执行时间到物资需求申请单
			 BaseDAO baseDAO = new BaseDAO();
			 for(PraybillVO aggvo :  retValue) {
				 Integer fbillstatus = aggvo.getHVO().getFbillstatus();
				 PraybillItemVO[] bvos = aggvo.getBVO();
				 String csourcetypecode = bvos[0].getCsourcetypecode();
				 if(fbillstatus == 3 && "422X".equals(csourcetypecode)) {
					String userId = InvocationInfoProxy.getInstance().getUserId();
				    String sql = "select pk_psndoc from sm_user where cuserid = '"+userId+"'";
				    String pk_psndoc = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
					List<StoreReqAppItemVO> sbvoList = new ArrayList<>();
					for(PraybillItemVO bvo : bvos) {
						String csourcebid = bvo.getCsourcebid();
					    StoreReqAppItemVO sbvo = (StoreReqAppItemVO)baseDAO.retrieveByPK(StoreReqAppItemVO.class, csourcebid);
					    sbvo.setVbdef16(new UFDate().toString());
				    	sbvo.setVbdef15(pk_psndoc);
				    	
				    	//回写扣库数量 lxiang20240112
				    	UFDouble nnum = bvo.getNnum();
						UFDouble nkksl = obj2UFDouble(sbvo.getVbdef6());//累计扣库数量  出库数量+形态转换数量
						sbvo.setVbdef6(nkksl.add(nnum).toString());
						
				    	
				    	sbvoList.add(sbvo);
					}
			    	baseDAO.updateVOList(sbvoList);

					
					//回写最近一次扣库处理人和时间  lxiang20240112
			    	StoreReqAppHeaderVO shvo = (StoreReqAppHeaderVO)baseDAO.retrieveByPK(StoreReqAppHeaderVO.class, bvos[0].getCsourceid());
			    	shvo.setVdef13(new UFDate().toString());
			    	shvo.setVdef12(pk_psndoc);
			    	baseDAO.updateVO(shvo);
				 }
			 }
			
			return retValue;
		} catch (Exception var5) {
			if (var5 instanceof BusinessException) {
				throw (BusinessException) var5;
			} else {
				throw new PFBusinessException(var5.getMessage(), var5);
			}
		}
	}
	
	private UFDouble obj2UFDouble(Object obj){
		return obj == null ? UFDouble.ZERO_DBL:new UFDouble(obj.toString());
	}
}